package cn.csc.module.distribute.controller.admin.bank;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.distribute.controller.admin.bank.vo.*;
import cn.csc.module.distribute.convert.bank.BankInfoConvert;
import cn.csc.module.distribute.dal.dataobject.bank.BankInfoDO;
import cn.csc.module.distribute.service.bank.BankInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 银行卡信息")
@RestController
@RequestMapping("/distribute/bank-info")
@Validated
public class BankInfoController {

    @Resource
    private BankInfoService bankInfoService;

    @PostMapping("/create")
    @ApiOperation("创建银行卡信息")
    @PreAuthorize("@ss.hasPermission('distribute:bank-info:create')")
    public CommonResult<BigInteger> createBankInfo(@Valid @RequestBody BankInfoCreateReqVO createReqVO) {
        return success(bankInfoService.createBankInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新银行卡信息")
    @PreAuthorize("@ss.hasPermission('distribute:bank-info:update')")
    public CommonResult<Boolean> updateBankInfo(@Valid @RequestBody BankInfoUpdateReqVO updateReqVO) {
        bankInfoService.updateBankInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除银行卡信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:bank-info:delete')")
    public CommonResult<Boolean> deleteBankInfo(@RequestParam("id") BigInteger id) {
        bankInfoService.deleteBankInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得银行卡信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:bank-info:query')")
    public CommonResult<BankInfoRespVO> getBankInfo(@RequestParam("id") BigInteger id) {
        BankInfoDO bankInfo = bankInfoService.getBankInfo(id);
        return success(BankInfoConvert.INSTANCE.convert(bankInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得银行卡信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('distribute:bank-info:query')")
    public CommonResult<List<BankInfoRespVO>> getBankInfoList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<BankInfoDO> list = bankInfoService.getBankInfoList(ids);
        return success(BankInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得银行卡信息分页")
    @PreAuthorize("@ss.hasPermission('distribute:bank-info:query')")
    public CommonResult<PageResult<BankInfoRespVO>> getBankInfoPage(@Valid BankInfoPageReqVO pageVO) {
        PageResult<BankInfoDO> pageResult = bankInfoService.getBankInfoPage(pageVO);
        return success(BankInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出银行卡信息 Excel")
    @PreAuthorize("@ss.hasPermission('distribute:bank-info:export')")
    @OperateLog(type = EXPORT)
    public void exportBankInfoExcel(@Valid BankInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BankInfoDO> list = bankInfoService.getBankInfoList(exportReqVO);
        // 导出 Excel
        List<BankInfoExcelVO> datas = BankInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "银行卡信息.xls", "数据", BankInfoExcelVO.class, datas);
    }

}
